﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using PromoServer.Library.Entities;
using PromoServer.Library;

namespace PromoServer_WPF
{
    /// <summary>
    /// Interaction logic for ImageCropperWindow.xaml
    /// </summary>
    public partial class ImageCropperWindow : Window
    {
        string currentImageFilePath;
        LayoutContent currentLayout;

        public ImageCropperWindow(LayoutContent layout)
        {
            InitializeComponent();

            currentLayout = layout;
            currentImageFilePath = currentLayout.ContentFileName;
            this.UcImageCropper.ImageUrl = currentImageFilePath;

            ValidateImage();
        }

        public ImageCropperWindow(LayoutContent layout, string imageFilePath)
        {
            InitializeComponent();

            currentLayout = layout;
            currentImageFilePath = imageFilePath;
            this.UcImageCropper.ImageUrl = currentImageFilePath;

            ValidateImage();
        }

        private void ValidateImage()
        {
            try
            {
                // Get current image width & height
                BitmapImage bmpImg = new BitmapImage(new Uri(currentImageFilePath));
                double width = bmpImg.Width;
                double height = bmpImg.Height;
                double ratio = Math.Round(width / height, 2);
                
                // Verify 16:9 aspect ration
                if (ratio != 1.78)
                {
                    this.imgMessage.Source = (ImageSource)FindResource("WarningImage");
                    this.lblMessage.Content = "The current image does not fit your devices screen. Drag the rectangle and press Crop Image to adjust the image to fit the devices screen.";
                    this.saveCroppedImageButton.Visibility = Visibility.Visible;
                }
                else
                {
                    this.imgMessage.Source = (ImageSource)FindResource("GreenCheckImage");
                    this.lblMessage.Content = "Image is ok and does not need to be cropped.";
                    this.saveCroppedImageButton.Visibility = Visibility.Hidden;
                }

            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("ImageCropperWindow.ValidateImage", ex.Message, true);
            }
        }

        private void saveCroppedImageButton_Click(object sender, RoutedEventArgs e)
        {
            string croppedImagePath = this.UcImageCropper.SaveNewCroppedImage(currentImageFilePath);
            string savedCroppedImagePath = SaveCroppedTempImageFile(croppedImagePath);
            currentImageFilePath = savedCroppedImagePath;

            if (savedCroppedImagePath != string.Empty)
            {
                // Set the cropped URL to cropped image path
                this.UcImageCropper.ImageUrl = savedCroppedImagePath;

                // Set the updated layout
                currentLayout.ContentFileName = savedCroppedImagePath;
            }

            ValidateImage();
        }

        private string SaveCroppedTempImageFile(string imageFilePath)
        {
            try
            {
                // If the original file path is temporary, delete it to prevent rogue files - REMOVED DUE TO LOCKING W/ "temp" CROPPER FILE
                //if (!isOriginalFileSaved)
                //{
                //    if (File.Exists(currentImageFilePath))
                //        File.Delete(currentImageFilePath);
                //}

                FileInfo fi = new FileInfo(imageFilePath);
                string newFilePath = string.Empty;
                newFilePath = System.AppDomain.CurrentDomain.BaseDirectory;
                newFilePath += Properties.Settings.Default.Images_Directory;

                // If images directory does not exist, create it
                if (!Directory.Exists(newFilePath))
                    Directory.CreateDirectory(newFilePath);

                string newFileName = "tmp_" + Guid.NewGuid().ToString() + fi.Extension.ToLower();

                newFilePath += newFileName;

                File.Copy(imageFilePath, newFilePath);

                return newFilePath;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("ImageCropperWindow.SaveCroppedTempImageFile", ex.Message, true);

                // Display error to user
                MessageBox.Show("Unable to save cropped image. Error: " + ex.Message);

                return string.Empty;
            }
        }

        private void UcImageCropper_Unloaded(object sender, RoutedEventArgs e)
        {
        }
    }
}
